package com.sg.web;

import com.github.pagehelper.PageInfo;
import com.sg.bean.*;
import com.sg.service.ClothingService;
import com.sg.service.ClothingStateService;
import com.sg.service.UserService;
import com.sg.util.NewFile;
import com.sg.vo.ChartInfo;
import com.sg.vo.ClothingVO;
import com.sg.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zh
 * 捐赠列表
 */
@Controller
public class ClothingController {
    Logger logger = Logger.getLogger(ClothingController.class);
    @Autowired
    ClothingService clothingService;
    @Autowired
    ClothingStateService clothingStateService;
    @Autowired
    UserService userService;

    /* @RequestMapping("/admin/clothinglist")
     public String clothinglist(Model model){

         List<ClothingVO> list=clothingService.findAll();
         logger.info(list);
         model.addAttribute("clothinglist",list);
         return "admin/clothinglist";
     }*/
    /*分页*/
    @RequestMapping("admin/clothinglist")
    public String clothingpage(String pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model) {
        //1.获得当前页码，和查询条件
        //2.判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        if (pageNum == null || "".equals(pageNum)) {
            pageNum = "1";
        }
        //查询到一个集合,pageNum页码pageSize
        List<ClothingVO> voList = clothingService.findAllPage(Integer.parseInt(pageNum), pageSize);
        //4.把集合数据封装到一个对象page中
        PageInfo pageInfo = new PageInfo(voList);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/admin/clothinglist.action");
        //把page对象储存到Model中，在当前页面取出
        model.addAttribute("page", page);
        //返回jsp页面
        return "admin/clothinglist";
    }


    /*用户列表*/
    @RequestMapping("clothinglist")
    public String clothingpages(String pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model) {
        //1.获得当前页码，和查询条件
        //2.判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        if (pageNum == null || "".equals(pageNum)) {
            pageNum = "1";
        }
        //查询到一个集合,pageNum页码pageSize
        List<ClothingVO> voList = clothingService.findAllPage(Integer.parseInt(pageNum), pageSize);
        //4.把集合数据封装到一个对象page中
        PageInfo pageInfo = new PageInfo(voList);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/clothinglist.action");
        //把page对象储存到Model中，在当前页面取出
        model.addAttribute("page", page);
        //返回jsp页面
        return "user/clothinglist";
    }

    /*进入未收货页面*/
    @RequestMapping("/admin/clothingtakeone")
    public String clothingtake(Model model) {
        List<ClothingVO> list = clothingService.findByStateName("待收取");
        logger.info(list);
        model.addAttribute("clothinglist", list);
        return "admin/clothingtake";
    }

    /*进入已收货页面*/
    @RequestMapping("/admin/clothingtaketwo")
    public String clothingtaketeo(Model model) {
        List<ClothingVO> list = clothingService.findByStateName("已收取");
        logger.info(list);
        model.addAttribute("clothinglist", list);

        return "admin/clothingtaketwo";
    }

    /*操作是否收货*/
    @RequestMapping("/admin/clothingtake")
    public String take(int clothingId) {
        Clothing clothing = clothingService.fingById(clothingId);
        clothing.setCsId(2);
        boolean b = clothingService.update(clothing);
        return "redirect:clothingtakeone.action";
    }

    /*删除*/
    @RequestMapping("clothingdelete")
    public String delete(int[] clothingId) {

        boolean flag = clothingService.delete(clothingId);
        logger.info(flag);
        return "redirect:clothinglist.action";
    }

    /*进入添加*/
    @RequestMapping("toclothingadd")

    public String toclothingadd(Model model, HttpSession session) {

        List<ClothingState> slist = clothingStateService.findAll();
        model.addAttribute("clothingstatelist", slist);
        session.getAttribute("createUser");

        return "user/clothingadd";
    }

    @RequestMapping("clothingadd")
    public String clothingadd(Clothing clothing,@RequestParam("file1") CommonsMultipartFile file1) throws IOException {
        logger.info("进入");
        if (file1 != null && file1.getSize() > 0) {
            File newFile = NewFile.getFile(file1);
            logger.info("logo"+newFile);
            file1.transferTo(newFile);//上传到目标位置
            clothing.setColthingImg(NewFile.getNewFileNameAnd(newFile.getName()));
            logger.info(clothing.getColthingImg());
        }
        logger.info(clothing);
        boolean b = clothingService.add(clothing);
        return "redirect:clothinglist.action";
    }


// /*   统计图*/
//    @RequestMapping("/admin/tocharts")
//    public String toCharts(){
//        logger.info("进入了图表控制器");
//        return "admin/clothingchart";
//    }
//
//    @RequestMapping("/admin/chartfirst")
//    @ResponseBody
//    public List<ChartInfo> chartFirst(){
//        List<ChartInfo> list = clothingService.findStatPie();
//        return list;
//    }
//
//    @RequestMapping("/admin/chartsecode")
//    @ResponseBody
//    public Map<String, List> chartSecode(){
//        Map<String,List> map = clothingService.findStatBar();
//        return map;
//    }

    /*公益救助站*/
    @RequestMapping("/user/clothingjuankuan")
    public String juankuan() {
        return "user/clothingjuankuan";
    }

    @RequestMapping("/user/clothingjuankuantwo")
    public String clothingjuankuantwo() {
        return "user/clothingjuankuantwo";
    }


}

